import { pick as _pick, get as _get } from 'lodash-es'
import moment from 'moment'
import 'moment/locale/zh-cn'
moment.locale('zh-cn')

// 生成需要保存到接口的数据
export default function (schema) {
  const formId = _get(schema, 'metaData._id')
  const metaData = _get(schema, 'metaData')
  const baseConf = _get(schema, 'baseConf')
  const bannerConf = _get(schema, 'bannerConf')
  const questionDataList = _get(schema, 'questionDataList')
  // console.log('schema', schema)
  // console.log('baseConf', baseConf)
  const title = (metaData?.title || '').replace(/<[^>]*>/g, '');
  const formData = {
    /**
     * 填报结束时间，格式同上
     */
    endTime: baseConf.endTime ? moment((baseConf.endTime)).format('YYYY-MM-DD HH:mm') : '',
    /**
     * 自定义表单名称
     */
    formName: title || metaData.formName,
    /**
     * 自定义表单说明
     */
    formRemark: bannerConf.titleConfig.mainTitle || metaData.formRemark,
    /**
     * 自定义表单类型
     */
    formType: metaData.formType,
    /**
     * 是否允许删除结果
     */
    isDel: (baseConf.isDel ? '1': '0') || metaData.isDel,
    /**
     * 是否鉴权
     */
    isLogin: (baseConf.anonymous ? '0': '1') || metaData.isLogin,
    /**
     * 是否允许提交多份
     */
    isMultiple: (baseConf.isMultiple ? '1': '0') || metaData.isMultiple,
    /**
     * 消息置顶
     */
    isTop: (baseConf.isTop ? '1': '') || metaData.isTop,
    /**
     * 是否允许修改结果
     */
    isUpdate: (baseConf.isUpdate ? '1': '0') || metaData.isUpdate,
    /**
     * 父表单ID，一级节点为 -1；
     */
    parentId: metaData.parentId,
    /**
     * 角色
     */
    roleType: metaData.roleType,
    /**
     * 填报开始时间
     */
    startTime: baseConf.begTime ? moment((baseConf.begTime)).format('YYYY-MM-DD HH:mm') : '',
    /**
     * 状态
     */
    status: metaData.status,
    /**
     * 使用权限
     */
    useLimit: baseConf?.answerUser?.length ? baseConf.answerUser[0]?.id : metaData.useLimit,
    listConf: (questionDataList?.map(itm => itm.title) || []).join(',') || title || metaData.formName,
  }

  const configData = _pick(schema, [
    'bannerConf',
    'baseConf',
    'bottomConf',
    'skinConf',
    'submitConf',
    'questionDataList',
    'logicConf'
  ])
  
  let dataFormAttributeList = []
  let dataFormAttrList = (metaData?.dataFormAttributeList || []).map(itm => {
    let attrContent
    try {
      attrContent = JSON.parse(itm?.attrContent || '{}')
    } catch (error) {
      attrContent = {}
    }
    return {
      attrId: itm.attrId,
      field: attrContent?.field || ''
    }
  })
  
  configData.questionDataList.forEach((item, index) => {
    let tmpidx = dataFormAttrList.findIndex(itm => itm.field === item.field)
    let obj = {
      attrContent: JSON.stringify(item),
      attrName: item.title,
      attrOrder: index,
      attrRemark: item.placeholderDesc || item.title,
      attrType: item.type,
      busineType: '1',
      extendType: '1',
      isRequired: item.isRequired ? '1' : '0',
      validate: item.valid,
      status: '1'
    }
    if (tmpidx > -1) {
      item.attrId = dataFormAttrList[tmpidx].attrId
      obj.attrId = dataFormAttrList[tmpidx].attrId
    }
    dataFormAttributeList.push(obj)
  })
  configData.dataConf = {
    dataList: configData.questionDataList
  }

  delete configData.questionDataList

  return {
    formId,
    ...formData,
    dataFormAttributeList: dataFormAttributeList,
    skinConf: JSON.stringify(configData)
  }
}
